πŸ” Nested Loops in C Programming (With Examples, Dry Run, Pattern Outputs & Line-by-Line Comments)

πŸ“˜ Part of the C Programming Beginner Series by LEARNING GROWTH HUB
✍️ Written by Krishna Popat


πŸš€ Introduction

In C programming, sometimes a single loop is not enough.

If you want to:

  • Print star patterns ⭐

  • Work with rows and columns

  • Create multiplication tables

  • Solve matrix problems

  • Build number designs

πŸ‘‰ You need Nested Loops.

Nested loops are one of the most powerful concepts in C programming.
If you master this topic, pattern programs and 2D array problems become much easier to solve.

Let’s understand everything step by step.


πŸ“Œ What is a Nested Loop?

A Nested Loop means:

A loop inside another loop.

πŸ‘‰ General Structure of Nested Loop:

for(initialization; condition; increment) // Outer loop { for(initialization; condition; increment) // Inner loop { // statements } }

The outer loop runs first.
For every execution of the outer loop, the inner loop runs completely.


🌍 Real-Life Example

Imagine:

  • 3 Students (Rows)

  • 2 Subjects (Columns)

For each student, you check both subjects.

Outer Loop → Students
Inner Loop → Subjects

Exactly how nested loops work.


πŸ”Ž Execution Formula

If the outer loop runs 3 times
And the inner loop runs 2 times

Total execution = 3 × 2 = 6

πŸ‘‰ Let’s understand nested loops with a simple example:


πŸ’» Program 1: Simple Nested Loop 

#include <stdio.h> // Include standard input-output library int main() // Main function starts { int i, j; // Declare two integer variables for(i = 1; i <= 3; i++) // Outer loop runs from 1 to 3 { for(j = 1; j <= 2; j++) // Inner loop runs from 1 to 2 { printf("i = %d, j = %d\n", i, j); // Print values } // End inner loop } // End outer loop return 0; // Program ends successfully }

✅ Output

i = 1, j = 1 i = 1, j = 2 i = 2, j = 1 i = 2, j = 2 i = 3, j = 1 i = 3, j = 2

🧠 Dry Run with Step Table

For:

for(i = 1; i <= 2; i++) { for(j = 1; j <= 3; j++)

Step-by-Step Execution Table (Dry Run)

Step i Value j Value Output
1 1 1 *
2 1 2 *
3 1 3 *
4 2 1 *
5 2 2 *
6 2 3 *

Total executions = 2 × 3 = 6

πŸ‘‰ This proves inner loop completes fully for each outer loop iteration.


⭐ Program 2: Square Star Pattern

#include <stdio.h> // Include input-output library int main() // Program starts { int i, j; // Declare loop variables for(i = 1; i <= 4; i++) // Outer loop controls rows { for(j = 1; j <= 4; j++) // Inner loop controls columns { printf("* "); // Print star } // End inner loop printf("\n"); // Move to next line } // End outer loop return 0; // End program }

✅ Pattern Output (Square)

* * * * * * * * * * * * * * * *

Rows = 4
Columns = 4


πŸ”Ί Program 3: Right Triangle Pattern 

#include <stdio.h> // Include standard library int main() // Main function { int i, j; // Declare loop variables for(i = 1; i <= 5; i++) // Control rows { for(j = 1; j <= i; j++) // Stars increase each row { printf("* "); // Print star } // End inner loop printf("\n"); // Next line } // End outer loop return 0; // End program }

✅ Pattern Output (Right Triangle)

* * * * * * * * * * * * * * *

Stars increase in every row.


πŸ”Ί Program 4: Pyramid Pattern 

#include <stdio.h> // Include standard library int main() // Main function { int i, j, space; // Declare variables for(i = 1; i <= 5; i++) // Control rows { for(space = 1; space <= 5 - i; space++) // Print spaces { printf(" "); // Print space } for(j = 1; j <= (2*i - 1); j++) // Print stars { printf("*"); // Print star } printf("\n"); // Move to next line } return 0; // End program }

✅ Pattern Output (Pyramid)

* *** ***** ******* *********

Spaces decrease
Stars increase


πŸ”’ Program 5: Multiplication Table

#include <stdio.h> // Include standard library int main() // Main function { int i, j; // Declare variables for(i = 1; i <= 5; i++) // Table numbers { for(j = 1; j <= 10; j++) // Multiply from 1 to 10 { printf("%d ", i * j); // Print multiplication result } printf("\n"); // Next line } return 0; // End program }

✅ Output (First 3 Tables Example)

1 2 3 4 5 6 7 8 9 10 2 4 6 8 10 12 14 16 18 20 3 6 9 12 15 18 21 24 27 30 ...

⚠ Common Mistakes

❌ Infinite loop
❌ Wrong condition
❌ Confusing rows & columns
❌ Missing braces
❌ Not understanding execution order


🎯 Practice Programs

  1. Inverted triangle

  2. Hollow square

  3. Number pyramid

  4. Floyd’s triangle

  5. 1–10 full multiplication tables

Before we move to practice problems, let’s quickly revise the most important concepts of Nested Loops.
The summary below will help you understand how nested loops work, why they are important, and where they are used in real programming.


This quick revision table gives you a complete overview of nested loops in one place.

If you clearly understand:

1) How the outer loop works

2) How the inner loop executes

3) How total execution is calculated

Then solving pattern programs and matrix problems becomes much easier.

πŸ‘‰ Always remember: Track outer and inner loops separately while debugging.


🏁 Final Conclusion

Nested loops are one of the most important concepts in C programming.

They form the foundation for:

• Pattern printing
• Matrix operations
• 2D arrays
• Table generation
• Algorithm building

If you truly understand nested loops, you are no longer a beginner — you are moving toward intermediate-level programming.

πŸ’‘ Keep practicing. Try creating new patterns on your own.

πŸš€ Keep Learning. Keep Growing.


πŸ“˜ Part of the C Programming Beginner Series
🌐 Blog: LEARNING GROWTH HUB
✍️ Author: Krishna Popat


Comments

Popular posts from this blog

🌟 The Honest Journey of a Student: Learning, Failing, and Growing

“C Programming for Beginners: Master Variables, Data Types, and Memory (Bits Explained)”